local skel = fk.CreateSkill {
  name = "lb__haoling",
}

Fk:loadTranslationTable {
  ["lb__haoling"] = "号令",
  --[":lb__haoling"] = "每个角色的回合开始时，你可失去1点体力，然后令其执行一个额外回合，此回合的阶段改为你从摸牌、出牌、弃牌中选择至少X-1、至多X项，且你可以任意排序（X为你已损失的体力值）；若你选择了X项/仅1项，本轮此技能失效/下次选择时不可包含该项。",
  --8.30 回归本意
  [":lb__haoling"] = "每个角色的回合开始时，你可失去1点体力，从摸牌、出牌、弃牌阶段中选择至少X-1、至多X项并以任意顺序令其额外执行（X为你已损失的体力值）；若你选择了X项/仅1项，本轮此技能失效/下次选择时不可包含该项。",

  ["@lb__haoling-round"] = "号令",
  ["@lb__haoling"] = "号令",
  ["@lb__haoling-turn"] = "号令",

  ["#lb__haoling-jin"] = "禁选",
  ["#lb__haoling-ask"] = "号令：是否令%dest获得由你排序阶段的额外回合",
  ["#lb__haoling_sort"] = "号令：为%dest的%arg~%arg2个阶段排序",
  ["#lb__haoling_sort2"] = "号令：为%dest的%arg个阶段排序",
  ["#lb__haoling_phase"] = "号令阶段",

  ["$lb__haoling1"] = "强敌当前，及时调整战略。",
  ["$lb__haoling2"] = "你会为此付出代价。",
  ["$lb__haoling3"] = "必须抓住这次机会。",
  ["$lb__haoling4"] = "时不再至，请助我一臂之力！",
}

skel:addEffect(fk.TurnStart, {
  anim_type = "masochism",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and data.reason ~= skel.name
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {
      skill_name = skel.name,
      prompt = "#lb__haoling-ask::" .. target.id,
    })
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:loseHp(player, 1, skel.name)
    if not player.dead then
      local all = { "phase_draw", "phase_play", "phase_discard" }
      if player:getMark("lb__haoling") ~= 0 then
        table.removeOne(all, player:getMark("lb__haoling"))
        room:setPlayerMark(player, "lb__haoling", 0)
        room:setPlayerMark(player, "@lb__haoling", 0)
      end
      local n = player:getLostHp()
      local ids = room:getTag(skel.name .. "_" .. player.id)
      local cards = table.map(all, function(name)
        for _, id in ipairs(ids) do
          local card = Fk:getCardById(id, true)
          if card.name == name then
            return id
          end
        end
      end)
      local min_num, max_num = math.min(n - 1, #all), math.min(n, #all)
      local prompt = "#lb__haoling_sort::" .. target.id .. ":" .. min_num .. ":" .. max_num
      if min_num == max_num then
        prompt = "#lb__haoling_sort2::" .. target.id .. ":" .. min_num
      end
      local get = room:askToArrangeCards(player, {
        skill_name = skel.name,
        card_map = {
          cards,
          skel.name, "#lb__haoling_phase" },
        prompt = prompt,
        box_size = 0,
        max_limit = { #all, max_num },
        min_limit = { 0, min_num },
        default_choice = { {}, table.slice(cards, 1, min_num + 1) },
      })[2]
      if #get == n then
        room:invalidateSkill(player, skel.name, "-round")
      end
      local phase_list = {
        ["phase_draw"] = "摸牌",
        ["phase_play"] = "出牌",
        ["phase_discard"] = "弃牌",
      }
      local list = table.map(get, function(id)
        local card = Fk:getCardById(id, true)
        return card.name
      end)
      if #list == 1 then
        room:setPlayerMark(player, "lb__haoling", list[1])
        room:setPlayerMark(player, "@lb__haoling", Fk:translate("#lb__haoling-jin") .. phase_list[list[1]])
      end
      if not target.dead and #list > 0 then
        local phase_table = table.map(list, function(phase)
          return Util.PhaseStrMapper(phase)
        end)
        room:setPlayerMark(target, "@lb__haoling-turn", table.concat(phase_list, "|"))
        for _, phase in ipairs(phase_table) do --8.30 回归本意
          if target.dead then return end
          target:gainAnExtraPhase(phase, skel.name, false)
        end
        --target:gainAnExtraTurn(false, skel.name, phase_table)
      end
    end
  end,
})

skel:addAcquireEffect(function(self, player, is_start)
  local room = player.room
  local ids = room:getTag(skel.name .. "_" .. player.id) or {}
  if #ids == 0 then
    for i, name in ipairs({ "phase_draw", "phase_play", "phase_discard" }) do
      local card = room:printCard(name, Card.Spade, i)
      table.insert(ids, card.id)
    end
    room:setTag(skel.name .. "_" .. player.id, ids)
  end
end)

skel:addLoseEffect(function(self, player, is_death)
  local room = player.room
  room:setPlayerMark(player, "lb__haoling", 0)
  room:setPlayerMark(player, "@lb__haoling", 0)
end)

return skel
